~ chicken-core (master) /manual/Module (chicken flonum)
Trap1[[tags: manual]]2[[toc:]]34== Module (chicken flonum)56Because CHICKEN supports a full numeric tower, operations can7sometimes incur a subtantial overhead to simply detect the type of numbers8you're passing in. When you know you're definitely dealing only with9flonums, you can choose to use flonum-specific operations to avoid10this overhead.1112This is purely a performance hack. You might want to consider adding13[[Types|type annotations]] instead, this often gives the same14performance boost without having to rewrite all numeric operators in15your code.1617=== Arithmetic floating-point operations1819<procedure>(fp+ X Y)</procedure>20<procedure>(fp- X Y)</procedure>21<procedure>(fp* X Y)</procedure>22<procedure>(fp/ X Y)</procedure>23<procedure>(fp*+ X Y Z)</procedure>24<procedure>(fpgcd X Y)</procedure>25<procedure>(fpneg X)</procedure>26<procedure>(fpmin X Y)</procedure>27<procedure>(fpmax X Y)</procedure>28<procedure>(fp= X Y)</procedure>29<procedure>(fp> X Y)</procedure>30<procedure>(fp< X Y)</procedure>31<procedure>(fp>= X Y)</procedure>32<procedure>(fp<= X Y)</procedure>33<procedure>(fpfloor X)</procedure>34<procedure>(fpceiling X)</procedure>35<procedure>(fptruncate X)</procedure>36<procedure>(fpround X)</procedure>37<procedure>(fpsin X)</procedure>38<procedure>(fpcos X)</procedure>39<procedure>(fptan X)</procedure>40<procedure>(fpasin X)</procedure>41<procedure>(fpacos X)</procedure>42<procedure>(fpatan X)</procedure>43<procedure>(fpatan2 X Y)</procedure>44<procedure>(fpsinh X)</procedure>45<procedure>(fpcosh X)</procedure>46<procedure>(fptanh X)</procedure>47<procedure>(fpasinh X)</procedure>48<procedure>(fpacosh X)</procedure>49<procedure>(fpatanh X)</procedure>50<procedure>(fplog X)</procedure>51<procedure>(fpexp X)</procedure>52<procedure>(fpexpt X Y)</procedure>53<procedure>(fpsqrt X)</procedure>54<procedure>(fpabs X)</procedure>55<procedure>(fpinteger? X)</procedure>5657Arithmetic floating-point operations.5859In safe mode, these procedures throw a type error when given non-float60arguments. In unsafe mode, these procedures do not check their61arguments. A non-flonum argument in unsafe mode can crash the62application. {{fp*+}} implements fused multiply-add {{(X * Y) + Z}}.6364Note: {{fpround}} uses the rounding mode that your C library65implements, which is usually different from R7RS.6667== Flonum limits6869<constant>maximum-flonum</constant><br>70<constant>minimum-flonum</constant><br>71<constant>flonum-radix</constant><br>72<constant>flonum-epsilon</constant><br>73<constant>flonum-precision</constant><br>74<constant>flonum-decimal-precision</constant><br>75<constant>flonum-maximum-exponent</constant><br>76<constant>flonum-minimum-exponent</constant><br>77<constant>flonum-maximum-decimal-exponent</constant><br>78<constant>flonum-minimum-decimal-exponent</constant><br>7980Platform-specific flonum limits.8182<procedure>(flonum-print-precision [PRECISION])</procedure>8384Gets and sets the number of significant digits printed for a floating-point85number. {{PRECISION}} must be a positive {{fixnum}}. Returns86the setting that was previously in effect.8788The default print precision is 15 on nearly all systems, and 789on the rare system on which the {{double}} type is only single-precision.9091'''Note:''' To ensure read/write invariance for ''all'' floating-point92numbers, you must increase print precision from 15 to 17 (or from 7 to939). For example:9495 > (define a (expt 2 -53))96 > (define b (+ a (* 2 (expt 10 -32))))97 > (eqv? a b)98 #f99 > (flonum-print-precision 15)100 > (cons a b)101 (1.11022302462516e-16 .102 1.11022302462516e-16) ;; same printed representation103 > (flonum-print-precision 17)104 > (cons a b)105 (1.1102230246251565e-16 .106 1.1102230246251568e-16) ;; differs in last place107108On the downside, this will result in unnecessarily precise109representations of many numbers:110111 > (flonum-print-precision 17)112 > 0.1113 0.10000000000000001114115The maximum number of decimal digits required to uniquely represent116all floating-point numbers of a certain precision is given by the117formula {{ceil(1+N*log10(2))}}, where N is the number of bits of118precision; for double-precision, {{N=53}}.119120121---122Previous: [[Module (chicken fixnum)]]123124Next: [[Module (chicken foreign)]]